<?php
//商品
namespace app\supplier\model;

use think\Model;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Product extends Model{
    protected $type = [
        'img_json' => 'json',
        'details_img_json' => 'json',
    ];
    // 模型初始化
    protected static function init()
    {
        parent::init();
        //TODO:初始化内容
    }
    
    //商品列表
    public static function productList($where,$page,$num){
        $list = self::alias('p')
        ->field('p.*,sum(o.order_quantity) as ocount')
        ->leftJoin("order o","o.p_id=p.id")
        ->where($where)
        ->group("o.p_id")
        ->order('p.id desc')
        ->limit(($page-1)*$num,$num)
        ->select();
        return $list;
    }
    
    //商品详情
    public static  function productInfo($where){
        $info = self::where($where)
         ->find();
        return $info;
    }
    
    //商品数
    public static function productCount($where) {
        return self::alias('p')
        ->leftJoin("order o","o.p_id=p.id")
        ->where($where)
        ->group("o.p_id")
        ->count();
    }

//    //商品修改
//    public static function productEdit($id,$update){
//        $in = self::update($update, ['id' => $id]);
//        return $in;
//    }
//    
//    //商品SKU修改
//    public static function productSKUEdit($id,$update){
//        $in = self::update($update, ['tie_up_pid' => $id]);
//        return $in;
//    }
//    
//    //商品批量修改
//    public function productUpdateAll($where,$data){
//        return $this::update($data, $where);
//    }
//
//    //商品添加
//    public static function productAdd(array $data){
//        return self::create($data);
//    }
//    
//    public static function ruleSql($sql){
//        return self::query($sql);
//    }
    
//    public static function productInc($where,$field,$num){
//        return self::where($where)->inc($field,$num)->update();
//    }
//
//    public static function productDec($where,$field,$num){
//        return self::where($where)->dec($field,$num)->update();
//    }
}
